using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;

namespace Data_Access_Tier
{
	public class KEYCODEData : DatabaseObjectBase
	{
		public KEYCODEData()
		{
			Initialize(true);
		}
		public KEYCODEData(Boolean newConnection)
		{
			Initialize(newConnection);
		}

		public Int32 KEYCODE_Insert(Int32 Id_Key, String KeyCode, Int32 Id_Contact, DateTime DateCreate, String Property, Int32 NumActive, Int32 Activated, Boolean Blacklist, Int32 TypeActive, Int32 TypeSoft, DateTime DateLimit, Boolean Register)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.InsertCommand = new SqlCommand("KEYCODE_Insert");
				_dbadapter.InsertCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.InsertCommand.Connection = _dbconn;
				_trans = _dbconn.BeginTransaction();
				_dbadapter.InsertCommand.Transaction = _trans;
				_dbadapter.InsertCommand.Parameters.Clear();
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Id_Key", SqlDbType.Int));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@KeyCode", SqlDbType.VarChar, 500));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Id_Contact", SqlDbType.Int));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@DateCreate", SqlDbType.DateTime));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Property", SqlDbType.NVarChar, 500));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@NumActive", SqlDbType.Int));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Activated", SqlDbType.Int));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Blacklist", SqlDbType.Bit));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@TypeActive", SqlDbType.Int));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@TypeSoft", SqlDbType.Int));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@DateLimit", SqlDbType.DateTime));
				_dbadapter.InsertCommand.Parameters.Add(new SqlParameter("@Register", SqlDbType.Bit));
				_dbadapter.InsertCommand.Prepare();
				SqlInt32 Id_KeySQL = Id_Key;
				_dbadapter.InsertCommand.Parameters["@Id_Key"].Value = Id_KeySQL;
				SqlString KeyCodeSQL = KeyCode;
				_dbadapter.InsertCommand.Parameters["@KeyCode"].Value = KeyCodeSQL;
				SqlInt32 Id_ContactSQL = Id_Contact;
				_dbadapter.InsertCommand.Parameters["@Id_Contact"].Value = Id_ContactSQL;
				SqlDateTime DateCreateSQL = SqlDateTime.Null;
				if (DateCreate > DateTime.MinValue)
				{
					try
					{
						DateCreateSQL = DateCreate;
					}
					catch (Exception eDateCreate)
					{
						String DateCreateMessage = eDateCreate.Message;
						DateCreateSQL = SqlDateTime.Null;
					}
				}
				_dbadapter.InsertCommand.Parameters["@DateCreate"].Value = DateCreateSQL;
				SqlString PropertySQL = Property;
				_dbadapter.InsertCommand.Parameters["@Property"].Value = PropertySQL;
				SqlInt32 NumActiveSQL = NumActive;
				_dbadapter.InsertCommand.Parameters["@NumActive"].Value = NumActiveSQL;
				SqlInt32 ActivatedSQL = Activated;
				_dbadapter.InsertCommand.Parameters["@Activated"].Value = ActivatedSQL;
				SqlBoolean BlacklistSQL = Blacklist;
				_dbadapter.InsertCommand.Parameters["@Blacklist"].Value = BlacklistSQL;
				SqlInt32 TypeActiveSQL = TypeActive;
				_dbadapter.InsertCommand.Parameters["@TypeActive"].Value = TypeActiveSQL;
				SqlInt32 TypeSoftSQL = TypeSoft;
				_dbadapter.InsertCommand.Parameters["@TypeSoft"].Value = TypeSoftSQL;
				SqlDateTime DateLimitSQL = SqlDateTime.Null;
				if (DateLimit > DateTime.MinValue)
				{
					try
					{
						DateLimitSQL = DateLimit;
					}
					catch (Exception eDateLimit)
					{
						String DateLimitMessage = eDateLimit.Message;
						DateLimitSQL = SqlDateTime.Null;
					}
				}
				_dbadapter.InsertCommand.Parameters["@DateLimit"].Value = DateLimitSQL;
				SqlBoolean RegisterSQL = Register;
				_dbadapter.InsertCommand.Parameters["@Register"].Value = RegisterSQL;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.InsertCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: KEYCODE_Insert: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: KEYCODE_Insert: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.InsertCommand != null)
					_dbadapter.InsertCommand.Dispose();
				CloseConnection();
			}
		}
		public Int32 KEYCODE_Update(Int32 Id_Key, String KeyCode, Int32 Id_Contact, DateTime DateCreate, String Property, Int32 NumActive, Int32 Activated, Boolean Blacklist, Int32 TypeActive, Int32 TypeSoft, DateTime DateLimit, Boolean Register)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.UpdateCommand = new SqlCommand("KEYCODE_Update");
				_dbadapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.UpdateCommand.Connection = _dbconn;

				_trans = _dbconn.BeginTransaction();
				_dbadapter.UpdateCommand.Transaction = _trans;

				_dbadapter.UpdateCommand.Parameters.Clear();
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Id_Key", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@KeyCode", SqlDbType.VarChar, 500));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Id_Contact", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@DateCreate", SqlDbType.DateTime));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Property", SqlDbType.NVarChar, 500));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@NumActive", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Activated", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Blacklist", SqlDbType.Bit));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@TypeActive", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@TypeSoft", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@DateLimit", SqlDbType.DateTime));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Register", SqlDbType.Bit));
				_dbadapter.UpdateCommand.Prepare();
				SqlInt32 Id_KeySQL = Id_Key;
				_dbadapter.UpdateCommand.Parameters["@Id_Key"].Value = Id_KeySQL;
				SqlString KeyCodeSQL = KeyCode;
				_dbadapter.UpdateCommand.Parameters["@KeyCode"].Value = KeyCodeSQL;
				SqlInt32 Id_ContactSQL = Id_Contact;
				_dbadapter.UpdateCommand.Parameters["@Id_Contact"].Value = Id_ContactSQL;
				SqlDateTime DateCreateSQL = SqlDateTime.Null;
				if (DateCreate > DateTime.MinValue)
				{
					try
					{
						DateCreateSQL = DateCreate;
					}
					catch (Exception eDateCreate)
					{
						String DateCreateMessage = eDateCreate.Message;
						DateCreateSQL = SqlDateTime.Null;
					}
				}
				_dbadapter.UpdateCommand.Parameters["@DateCreate"].Value = DateCreateSQL;
				SqlString PropertySQL = Property;
				_dbadapter.UpdateCommand.Parameters["@Property"].Value = PropertySQL;
				SqlInt32 NumActiveSQL = NumActive;
				_dbadapter.UpdateCommand.Parameters["@NumActive"].Value = NumActiveSQL;
				SqlInt32 ActivatedSQL = Activated;
				_dbadapter.UpdateCommand.Parameters["@Activated"].Value = ActivatedSQL;
				SqlBoolean BlacklistSQL = Blacklist;
				_dbadapter.UpdateCommand.Parameters["@Blacklist"].Value = BlacklistSQL;
				SqlInt32 TypeActiveSQL = TypeActive;
				_dbadapter.UpdateCommand.Parameters["@TypeActive"].Value = TypeActiveSQL;
				SqlInt32 TypeSoftSQL = TypeSoft;
				_dbadapter.UpdateCommand.Parameters["@TypeSoft"].Value = TypeSoftSQL;
				SqlDateTime DateLimitSQL = SqlDateTime.Null;
				if (DateLimit > DateTime.MinValue)
				{
					try
					{
						DateLimitSQL = DateLimit;
					}
					catch (Exception eDateLimit)
					{
						String DateLimitMessage = eDateLimit.Message;
						DateLimitSQL = SqlDateTime.Null;
					}
				}
				_dbadapter.UpdateCommand.Parameters["@DateLimit"].Value = DateLimitSQL;
				SqlBoolean RegisterSQL = Register;
				_dbadapter.UpdateCommand.Parameters["@Register"].Value = RegisterSQL;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.UpdateCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: KEYCODE_Update: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: KEYCODE_Update: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.UpdateCommand != null)
					_dbadapter.UpdateCommand.Dispose();
				CloseConnection();
			}
		}
		public Int32 KEYCODE_Disable(Int32 Id_Key, ref Boolean Enabled)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.UpdateCommand = new SqlCommand("KEYCODE_Disable");
				_dbadapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.UpdateCommand.Connection = _dbconn;

				_trans = _dbconn.BeginTransaction();
				_dbadapter.UpdateCommand.Transaction = _trans;

				_dbadapter.UpdateCommand.Parameters.Clear();
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Id_Key", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@enabled", SqlDbType.Bit));
				_dbadapter.UpdateCommand.Prepare();
				SqlInt32 Id_KeySQL = Id_Key;
				_dbadapter.UpdateCommand.Parameters["@Id_Key"].Value = Id_KeySQL;
				_dbadapter.UpdateCommand.Parameters["@enabled"].Direction = ParameterDirection.Output;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.UpdateCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: KEYCODE_Disable: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: KEYCODE_Disable: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.UpdateCommand != null)
					_dbadapter.UpdateCommand.Dispose();
				CloseConnection();
			}
		}
		public Int32 KEYCODE_Delete(Int32 Id_Key)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.DeleteCommand = new SqlCommand("KEYCODE_Delete");
				_dbadapter.DeleteCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.DeleteCommand.Connection = _dbconn;

				_trans = _dbconn.BeginTransaction();
				_dbadapter.DeleteCommand.Transaction = _trans;

				_dbadapter.DeleteCommand.Parameters.Clear();
				_dbadapter.DeleteCommand.Parameters.Add(new SqlParameter("@Id_Key", SqlDbType.Int));
				_dbadapter.DeleteCommand.Prepare();
				SqlInt32 Id_KeySQL = Id_Key;
				_dbadapter.DeleteCommand.Parameters["@Id_Key"].Value = Id_KeySQL;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.DeleteCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: KEYCODE_Delete: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: KEYCODE_Delete: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.DeleteCommand != null)
					_dbadapter.DeleteCommand.Dispose();
				CloseConnection();
			}
		}
		public Int32 KEYCODE_Save(Int32 Id_Key, String KeyCode, Int32 Id_Contact, DateTime DateCreate, String Property, Int32 NumActive, Int32 Activated, Boolean Blacklist, Int32 TypeActive, Int32 TypeSoft, DateTime DateLimit, Boolean Register)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.UpdateCommand = new SqlCommand("KEYCODE_Save");
				_dbadapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.UpdateCommand.Connection = _dbconn;

				_trans = _dbconn.BeginTransaction();
				_dbadapter.UpdateCommand.Transaction = _trans;

				_dbadapter.UpdateCommand.Parameters.Clear();
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Id_Key", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@KeyCode", SqlDbType.VarChar, 500));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Id_Contact", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@DateCreate", SqlDbType.DateTime));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Property", SqlDbType.NVarChar, 500));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@NumActive", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Activated", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Blacklist", SqlDbType.Bit));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@TypeActive", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@TypeSoft", SqlDbType.Int));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@DateLimit", SqlDbType.DateTime));
				_dbadapter.UpdateCommand.Parameters.Add(new SqlParameter("@Register", SqlDbType.Bit));
				_dbadapter.UpdateCommand.Prepare();
				SqlInt32 Id_KeySQL = Id_Key;
				_dbadapter.UpdateCommand.Parameters["@Id_Key"].Value = Id_KeySQL;
				SqlString KeyCodeSQL = KeyCode;
				_dbadapter.UpdateCommand.Parameters["@KeyCode"].Value = KeyCodeSQL;
				SqlInt32 Id_ContactSQL = Id_Contact;
				_dbadapter.UpdateCommand.Parameters["@Id_Contact"].Value = Id_ContactSQL;
				SqlDateTime DateCreateSQL = SqlDateTime.Null;
				if (DateCreate > DateTime.MinValue)
				{
					try
					{
						DateCreateSQL = DateCreate;
					}
					catch (Exception eDateCreate)
					{
						String DateCreateMessage = eDateCreate.Message;
						DateCreateSQL = SqlDateTime.Null;
					}
				}
				_dbadapter.UpdateCommand.Parameters["@DateCreate"].Value = DateCreateSQL;
				SqlString PropertySQL = Property;
				_dbadapter.UpdateCommand.Parameters["@Property"].Value = PropertySQL;
				SqlInt32 NumActiveSQL = NumActive;
				_dbadapter.UpdateCommand.Parameters["@NumActive"].Value = NumActiveSQL;
				SqlInt32 ActivatedSQL = Activated;
				_dbadapter.UpdateCommand.Parameters["@Activated"].Value = ActivatedSQL;
				SqlBoolean BlacklistSQL = Blacklist;
				_dbadapter.UpdateCommand.Parameters["@Blacklist"].Value = BlacklistSQL;
				SqlInt32 TypeActiveSQL = TypeActive;
				_dbadapter.UpdateCommand.Parameters["@TypeActive"].Value = TypeActiveSQL;
				SqlInt32 TypeSoftSQL = TypeSoft;
				_dbadapter.UpdateCommand.Parameters["@TypeSoft"].Value = TypeSoftSQL;
				SqlDateTime DateLimitSQL = SqlDateTime.Null;
				if (DateLimit > DateTime.MinValue)
				{
					try
					{
						DateLimitSQL = DateLimit;
					}
					catch (Exception eDateLimit)
					{
						String DateLimitMessage = eDateLimit.Message;
						DateLimitSQL = SqlDateTime.Null;
					}
				}
				_dbadapter.UpdateCommand.Parameters["@DateLimit"].Value = DateLimitSQL;
				SqlBoolean RegisterSQL = Register;
				_dbadapter.UpdateCommand.Parameters["@Register"].Value = RegisterSQL;
				Int32 returnvalue = Convert.ToInt32(_dbadapter.UpdateCommand.ExecuteScalar());

				_trans.Commit();
				return returnvalue;
			}
			catch (SqlException e)
			{
				try
				{
					_trans.Rollback();
				}
				catch (SqlException ex)
				{
					throw new Exception("SQL Rollback Error: KEYCODE_Save: " + ex.Message, ex);
				}
				throw new Exception("SQL Error: KEYCODE_Save: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.UpdateCommand != null)
					_dbadapter.UpdateCommand.Dispose();
				CloseConnection();
			}
		}
		public DataSet KEYCODE_GetList()
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.SelectCommand = new SqlCommand("KEYCODE_GetList");
				_dbadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.SelectCommand.Connection = _dbconn;
				_dbadapter.SelectCommand.Parameters.Clear();
				_dbadapter.SelectCommand.Prepare();
				_dbadapter.Fill(_data);

				return _data;
			}
			catch (SqlException e)
			{
				throw new Exception("SQL Error: KEYCODE_GetList: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.SelectCommand != null)
					_dbadapter.SelectCommand.Dispose();
				CloseConnection();
			}
		}
		public DataSet KEYCODE_Get(Int32 Id_Key)
		{
			try
			{
				if (_dbconn.State != ConnectionState.Open)
					_dbconn.Open();
				_dbadapter.SelectCommand = new SqlCommand("KEYCODE_Get");
				_dbadapter.SelectCommand.CommandType = CommandType.StoredProcedure;
				_dbadapter.SelectCommand.Connection = _dbconn;
				_dbadapter.SelectCommand.Parameters.Clear();
				_dbadapter.SelectCommand.Parameters.Add(new SqlParameter("@Id_Key", SqlDbType.Int));
				_dbadapter.SelectCommand.Prepare();
				SqlInt32 Id_KeySQL = Id_Key;
				_dbadapter.SelectCommand.Parameters["@Id_Key"].Value = Id_KeySQL;
				_dbadapter.Fill(_data);

				return _data;
			}
			catch (SqlException e)
			{
				throw new Exception("SQL Error: KEYCODE_Get: " + e.Message, e);
			}
			finally
			{
				if (_dbadapter.SelectCommand != null)
					_dbadapter.SelectCommand.Dispose();
				CloseConnection();
			}
		}
	}
}
